package com.jxsz.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxsz.entity.CountVo;
import com.jxsz.entity.OrderQueryDto;
import com.jxsz.entity.TDept;
import com.jxsz.entity.TOrder;
import com.jxsz.mapper.TDeptMapper;
import com.jxsz.mapper.TOrderMapper;
import com.jxsz.service.TOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {
    @Autowired
    private TOrderMapper  tOrderMapper;
    @Autowired
    private TDeptMapper  tDeptMapper;

    @Override
    public Page<TOrder> findAllOrders(Integer page, Integer size, OrderQueryDto orderQueryDto) {
        Page<TOrder> page1 = new Page<>(page, size);
        LambdaQueryWrapper<TOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(orderQueryDto.getTitle()),TOrder::getTitle, orderQueryDto.getTitle());
        queryWrapper.eq(orderQueryDto.getHandleDeptId()>0,TOrder::getHandleDeptId, orderQueryDto.getHandleDeptId());
        return this.page(page1, queryWrapper);
    }

    @Override
    public void saveOrder(TOrder order) {

        if (order.getId()==null){
            //新增操作
            String orderNo = String.valueOf(System.currentTimeMillis())+ (new Random().nextInt(10000));
            order.setOrderNo(orderNo);
            order.setCreateTime(new Date());
            tOrderMapper.insert(order);
        }else {
            //修改操作
            tOrderMapper.updateById(order);
        }

    }

    @Override
    public Map<String, Object> fenpai(Integer id, Integer deptId, String deptName) {
        LambdaQueryWrapper<TDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TDept::getDeptId,deptId);
        TDept byId = tDeptMapper.selectOne(queryWrapper);
        if (byId==null){
            return new HashMap<String,Object>(){
                {
                    put("code",400);
                    put("msg","部门不存在");
                }
            };
        }
        String deptName1 = byId.getDeptName();
        if (!deptName1.equals(deptName)){
            return new HashMap<String,Object>(){
                {
                    put("code",400);
                    put("msg","部门信息有误");
                }
            };
        }
        TOrder order = tOrderMapper.selectById(id);
        order.setHandleDeptId(deptId);
        order.setFenpaiTime(new Date());

        this.saveOrder(order);
        return new HashMap<String,Object>(){
            {
                put("code",200);
                put("msg","分配成功");
            }
        };
    }

    @Override
    public Map<String, Object> queryDeptCount(Integer deptId,String startTime,String endTime) {
        LambdaQueryWrapper<TOrder>  queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TOrder::getHandleDeptId, deptId);
        queryWrapper.between(StringUtils.hasLength(startTime),TOrder::getFenpaiTime, startTime, endTime);
        Long allCount = tOrderMapper.selectCount(queryWrapper);
        queryWrapper.eq(TOrder::getIsOverdue, 1);
        Long overdueCount = tOrderMapper.selectCount(queryWrapper);
        return new HashMap<String,Object>(){
            {
                put("code",200);
                put("msg","success");
                put("data",new  HashMap<String,Object>(){
                    {
                        put("allCount",allCount);
                        put("overdueRate",overdueCount/allCount);
                    }
                });
            }
        };
    }

    @Override
    public Map<String, Object> queryTypeCount(Integer orderType, String startTime, String endTime) {
        LambdaQueryWrapper<TOrder>  queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TOrder::getOrderType, orderType);
        queryWrapper.between(StringUtils.hasLength(startTime),TOrder::getFenpaiTime, startTime, endTime);
        Long allCount = tOrderMapper.selectCount(queryWrapper);
        queryWrapper.eq(TOrder::getIsOverdue, 1);
        Long overdueCount = tOrderMapper.selectCount(queryWrapper);
        return new HashMap<String,Object>(){
            {
                put("code",200);
                put("msg","success");
                put("data",new  HashMap<String,Object>(){
                    {
                        put("allCount",allCount);
                        put("overdueRate",overdueCount/allCount);
                    }
                });
            }
        };
    }


}




